package com.org.gameloft.GLIDER.IDE;

import com.org.gameloft.GLIDER.common.GLIDEROptions;
import com.org.gameloft.GLIDER.common.GLLogger;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Aug 1, 2007
 * Time: 11:54:36 AM
 * Revision: 0
 */
public class GLIDERWindow extends JFrame
{
    private JMenuBar menuBar;
    private JToolBar toolBar;

    public GLIDERWindow()
    {
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setLocation(GLIDEROptions.getProperty(GLIDEROptions.WINDOW_X, 50), GLIDEROptions.getProperty(GLIDEROptions.WINDOW_Y, 50));
        setSize(GLIDEROptions.getProperty(GLIDEROptions.WINDOW_W, 600), GLIDEROptions.getProperty(GLIDEROptions.WINDOW_H, 400));
        setExtendedState(GLIDEROptions.getProperty(GLIDEROptions.WINDOW_STATE, NORMAL));

        menuBar = new JMenuBar();
        ActionManager.getActionManager().populateMenu(menuBar);
        setJMenuBar(menuBar);

        toolBar = new JToolBar();
//        toolBar.setFloatable(false);
        toolBar.setRollover(true);
        ActionManager.getActionManager().populateToolbar(toolBar);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(toolBar, BorderLayout.NORTH);

        setVisible(true);
        GLIDERMain.hideSplash();

        addWindowStateListener(new WindowStateListener()
        {
            public void windowStateChanged(WindowEvent e)
            {
                GLIDEROptions.setProperty(GLIDEROptions.WINDOW_STATE, getExtendedState() & (~ICONIFIED) );
            }
        });
        addWindowListener(new WindowAdapter()
        {
            public void windowClosed(WindowEvent e)
            {
            }

            public void windowClosing(WindowEvent e)
            {
                GLIDEROptions.save();
            }
        });

        addComponentListener(new ComponentAdapter()
        {
            public void componentMoved(ComponentEvent e)
            {
                if (getExtendedState() == NORMAL)
                {
                    GLIDEROptions.setProperty(GLIDEROptions.WINDOW_X, getX());
                    GLIDEROptions.setProperty(GLIDEROptions.WINDOW_Y, getY());
                }
            }

            public void componentResized(ComponentEvent e)
            {
                if (getExtendedState() == NORMAL)
                {
                    GLIDEROptions.setProperty(GLIDEROptions.WINDOW_W, getWidth());
                    GLIDEROptions.setProperty(GLIDEROptions.WINDOW_H, getHeight());
                }
            }
        });

        new ToolbarEditor();
    }

    public static void create()
    {
        try
        {
            UIManager.setLookAndFeel("com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
//            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e)
        {
            GLLogger.log(e);
        }
        new GLIDERWindow();
    }
}
